package grape.mybatisplus.generator;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import lombok.Data;
import lombok.experimental.Accessors;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by yangwei
 * Created at 2020/4/10 13:21
 */
@Data
@Accessors(chain = true)
public class CustomerDataSourceConfig extends DataSourceConfig {

    String publickKey;

    /**
     * 创建数据库连接对象
     *
     * @return Connection
     */
    public Connection getConn() {

        Connection conn = null;
        try {

            DruidDataSource datasource = new DruidDataSource();

            datasource.setUrl(getUrl());
            datasource.setUsername(getUsername());
            datasource.setPassword(getPassword());
            datasource.addConnectionProperty("config.decrypt","true");
            datasource.addConnectionProperty("config.decrypt.key",getPublickKey());
            datasource.setDriverClassName(getDriverName());
            datasource.addFilters("config");
            datasource.init();

            conn = datasource.getConnection();
        } catch ( SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}
